package MassageController.Massage;

import java.io.IOException;
import java.io.PrintWriter;

import MemberController.MLoginManager;
import MypageController.MyMain.MyMainDAO;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

/**
 * Servlet implementation class MassageServ
 */
public class MassageServ extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MassageServ() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	
 		request.setCharacterEncoding("UTF-8");
 		response.setCharacterEncoding("UTF-8");
 		response.setContentType("text/html; charset=UTF-8");
 		RequestDispatcher view = null;
 		
 		boolean isLogin=false;
 		PrintWriter out =response.getWriter();
 		
 		MassageBean[] massage = null;
 		MassageBean massageDeep = new MassageBean();
 		MyMainDAO mymaindao = new MyMainDAO();
 		MLoginManager manager = new  MLoginManager();
 		
 		String page = null;
 		String pageArea = null;
 		if(request.getParameter("page") != null){
 			page=request.getParameter("page");
 		}
 		if(request.getParameter("pageArea") != null){
 			pageArea=request.getParameter("pageArea");
 		}
 		
 		String action=request.getParameter("action");;
 		String id=request.getParameter("id");
 		MassageDAO massageDao = new MassageDAO();
 		
 		if(action.equals("MsList")){
 			
 			System.out.println("id = " + id);
 			
 			manager.getInstance();
 			
 			
 			
 				massage = massageDao.List(id, page);
 	 			
 	 			int massageAll = massageDao.ListCount(id);
 	 			
 	 			System.out.println("쪽지 총 수 = " + massageAll);
 	 			System.out.println("쪽지 에리어  = " + pageArea);
 	 	 					
 	 			request.setAttribute("Out", massage);
 	 			request.setAttribute("OutAll", massageAll);
 	 			request.setAttribute("OutPage", page);
 	 			request.setAttribute("OutPageArea", pageArea);
 	 			
 	 			view=request.getRequestDispatcher("/Massage/Ms_List.jsp");
 	 			view.forward(request,response); 
 	 	
 			}else if(action.equals("MsDelete")){
 			
 			for(int i = 0; i < 5; i++){
 				
 				String ch = request.getParameter("ch" + i);
 				
 				if(ch != null){
 					int chValue = Integer.parseInt(request.getParameter("chValue" + i));
 					massageDao.Listdelete(chValue);
 				}
 			}
 			System.out.println("지우고 페이지 번호 = " +page);
 			System.out.println("지우고 ID 번호 = " + id);
 			
 			massage = massageDao.List(id, page);
 			int massageAll = massageDao.ListCount(id);
 			
 			request.setAttribute("Out", massage);
 			request.setAttribute("OutAll", massageAll);
 			request.setAttribute("OutPage", page);
 			request.setAttribute("OutPageArea", pageArea);
 			
 			view=request.getRequestDispatcher("/Massage/Ms_List.jsp");
 			view.forward(request,response); 
 		}else if(action.equals("MsScript")){
 			
 			String chValue = request.getParameter("chValue");
 			massageDeep = massageDao.ScriptSee(chValue);
 			massageDao.MsCheck(chValue);
 			
 			request.setAttribute("Out", massageDeep);
 			request.setAttribute("OutPage", page);
 			request.setAttribute("OutPageArea", pageArea);
 			view=request.getRequestDispatcher("/Massage/Ms_Read.jsp");
 			view.forward(request,response); 
 		}else if(action.equals("MsWriteView")){
 			String to=request.getParameter("to");
 			
 			request.setAttribute("Out", id);
 			request.setAttribute("OutTo", to);
 			request.setAttribute("OutPage", page);
 			view=request.getRequestDispatcher("/Massage/Ms_Write.jsp");
 			view.forward(request,response); 
 		}else if(action.equals("MsWirte")){
 			
 			System.out.println("도착??");
 			
 			System.out.println(request.getParameter("To"));
 			System.out.println(request.getParameter("Form"));
 			System.out.println(request.getParameter("textarea"));
 			
 			massageDeep.setMsgTo(request.getParameter("To"));
 			massageDeep.setMsgForm(request.getParameter("Form"));
 			massageDeep.setMsgContents(request.getParameter("textarea"));
 			
 			massageDao.Write(massageDeep);	
 		}else if(action.equals("check")){
System.out.println("메세지 아작스");			
 			String result = null;
 			isLogin = massageDao.MsReadCheck(id);
 			
 			int level = mymaindao.level(id);
 			if(isLogin){
 				result = "1"; // 안 읽은 쪽지 있다.
 			}else{
 				result = "2"; // 인 읽은 쪽지 없다.
 			}
 			
 			
			JSONArray inner = new JSONArray();
			JSONObject obj = new JSONObject();
			obj.put("share", result);
			obj.put("level", level);

			out.println(obj);
			out.close();
 			
 		}
	}
}
